//
//  BDSettingDetailTableViewController.m
//  BeidouMall
//
//  Created by qie on 16/4/5.
//  Copyright © 2016年 qie. All rights reserved.
//

#import "BDSettingDetailTableViewController.h"
#import "BDFindPwdTableViewController.h"

@interface BDSettingDetailTableViewController () <UITableViewDelegate>

@end

@implementation BDSettingDetailTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"设置";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 5.0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
        case 0:
            if (indexPath.row == 0) {
                [self performSegueWithIdentifier:@"modifyPwdSegue" sender:nil];
            }
            break;
            
        default:
            break;
    }
}

#pragma mark IBActions

- (IBAction)logoutButtonPressed:(UIButton *)sender {
    NSString *url = [NSString stringWithFormat:@"%@%@?userId=%@",[BDNetworking shareInstance].baseUrl,LOGIN_OUT,[BDAccountManagerModel shareInstance].userId];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view withText:@"正在注销"];
    
    [[BDAccountManagerModel shareInstance] logoutWithUrl:url completionHandler:^(NSError *error) {
        if (error != nil) {
            if ([[error localizedDescription] isEqualToString:@"登录超时"] && error.code == 401) {
                hud.labelText = @"注销成功";
                [hud hideAfterDelay:0.5];
                // 清除webViw cookie
                NSHTTPCookie *cookie;
                NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
                for (cookie in [storage cookies])
                {
                    [storage deleteCookie:cookie];
                }
                
                [BDAccountManagerModel shareInstance].authorization = @"";
                [BDAccountManagerModel shareInstance].login = NO;
                
                dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC);
                dispatch_after(time, dispatch_get_main_queue(), ^{
                    [self.navigationController popToRootViewControllerAnimated:YES];
                });
            } else {
                NSLog(@"注销失败%@", [error localizedDescription]);
                hud.labelText = [error localizedDescription];
                [hud hideAfterDelay:0.5];
            }
        } else {
            hud.labelText = @"注销成功";
            [hud hideAfterDelay:0.5];
            // 清除webViw cookie
            NSHTTPCookie *cookie;
            NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
            for (cookie in [storage cookies])
            {
                [storage deleteCookie:cookie];
            }
            
            [BDAccountManagerModel shareInstance].authorization = @"";
            [BDAccountManagerModel shareInstance].login = NO;
            
            dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC);
            dispatch_after(time, dispatch_get_main_queue(), ^{
                [self.navigationController popToRootViewControllerAnimated:YES];
            });
        }
    }];
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"modifyPwdSegue"]) {
        BDFindPwdTableViewController *vc = segue.destinationViewController;
        
        NSDictionary *dic = @{@"userId":[BDAccountManagerModel shareInstance].userId};
        vc.userInfo = @{@"user":dic};
    }
}


@end
